﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using You.Niao.User.Model;
using You.Niao.User.IDAL;
using System.IO;
using AOP;

namespace You.Niao.User.Area.Controllers
{
    public class UserAPIController:Controller
    {

        public IUser Iuser;
        public IPrletter Iprletter;


        public UserAPIController(IUser _IUser, IPrletter _IPrletter)
        {
            Iuser = _IUser;
            Iprletter = _IPrletter;
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Create(Users data)
        {
           
            JsonResponse JR = new JsonResponse();
            JR.isSuccess = false;
            if (HttpContext.Session["CheckCode"] != null)
            {
                if (HttpContext.Session["CheckCode"].ToString().ToLower() != data.Code.ToLower())
                {
                    JR.Message = "验证码不正确";
                    return Json(JR);
                }
            }
            else
            {
                JR.Message = "验证码不正确";
                return Json(JR);
            }
            if (Utility.isEmail(data.Email))
            {
                JR.Message = "邮箱不正确";
                return Json(JR);
            }

            data.Secret=Guid.NewGuid().ToString();
            data.Pwd = Utility.EncryptMD5(data.Pwd);
            JR.isSuccess = Iuser.CreateUser(data);
            if (JR.isSuccess)
            {

                string Content = string.Format("亲爱的{0}，为方便及时联系到您，请点击下面的链接验证您的Email：<a href='{1}User/Verification/{2}'> {1}User/Verification/{2} </a>--游鸟网 ", data.NickName, Common.Utility.ReadWebConfig("SiteUrl", "SiteUrl"), data.Secret);


                SMTP.sentEmail(data.Email, "游鸟网注册认证", Content, Utility.ReadWebConfig("SendEmail", "SendEmail"), "SMTP", 25, true);


            }
            else
            {
                JR.Message = "注册失败,邮箱已被注册，或者昵称已给使用";
            }
            return Json(JR);
        }


        [HttpGet]
        public string  CheckNickName(string NickName)
        {
            return  Iuser.CheckNickName(NickName)?"1":"0" ;
        }

        [HttpGet]
        public string CheckEmail(string Email)
        {
            return Iuser.CheckEmail(Email) ? "1" : "0";
        }
        
        public JsonResult UserLogin(Users User,string isSave)
        {
            JsonResponse JR = new JsonResponse();
            User.Pwd=Common.Utility.EncryptMD5(User.Pwd);
            Users U = Iuser.UserLogin(User);

            if (U.UserID > 0)
            {
                switch (U.Status)
                {
                    case 0: JR.isSuccess = false;
                        JR.Message = "帐号不存在！"; break;
                    case 1: JR.isSuccess = true;
                  
                        CommonCookie.SaveCookie("Avatar", U.Avatar,0);
                        CommonCookie.SaveCookie("NickName",U.NickName,0);
                        CommonCookie.SaveCookie("UserID",Convert.ToString(U.UserID),0);
                        CommonCookie.SaveCookie("LetterNumber",U.LetterNumber.ToString(),0);
                        CommonCookie.SaveCookie("MessageNumber",U.MessageNumber.ToString(),0);
                        break;
                    case 2: JR.isSuccess = false;
                        JR.Message = "该帐号已锁定"; break;
                }
            }
            else
            {
                JR.isSuccess = false;
                JR.Message = "帐号或者密码错误";
            }

            var jsonmsg = new 
            {
                isSuccess=JR.isSuccess,
                Message=JR.Message,
                Nickname=U.NickName,
                Avatar = U.Avatar,
                LetterNumber=U.LetterNumber,
                MessageNumber=U.MessageNumber
            };

            return Json(jsonmsg);
        }

        /// <summary>
        /// 添加私信
        /// </summary>
        /// <param name="pl"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult AddMsg(PrLetter pl)
        {
            //CookieDESC user = new CookieDESC();
            int UserID;
            int.TryParse(CommonCookie.GetCookie("UserID"), out UserID);
            pl.SenderUserID = UserID;
            pl.Sender = CommonCookie.GetCookie("NickName");
            int number = Iprletter.Add(pl);
            return Json(number);
        }

        /// <summary>
        /// 更新个人资料
        /// </summary>
        /// <param name="users"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult Update(Users users)
        {
            int UserID;
            int.TryParse(CommonCookie.GetCookie("UserID"), out UserID);
            users.UserID = UserID;
            bool u = Iuser.Update(users);
            return Json(u);
        }
    }
}
